<!doctype html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>后台登录</title>
    <header th:replace="header::html"/>
    <link rel="stylesheet" th:href="@{/xadmin/css/login.css}">
</head>
<body class="login-bg">
    <div class="login layui-anim layui-anim-up">
        <div class="message">后台管理系统登录</div>
        <div id="darkbannerwrap"></div>

        <form method="post" class="layui-form">
            <input name="username" placeholder="用户名" type="text" lay-verify="required" class="layui-input">
            <hr class="hr15">
            <input name="password" lay-verify="required" placeholder="密码" type="password" class="layui-input">
            <hr class="hr15">
            <input value="登录" lay-submit lay-filter="login" style="width:100%;" type="submit">
            <hr class="hr20">
        </form>
    </div>

    <script>
        $(function () {
            layui.use('form', function () {
                var form = layui.form;

                //监听提交
                form.on('submit(login)', function (data) {
                    $.ajax({
                        url: '/login',
                        type: 'POST',
                        data: data.field,
                        dataType: 'json',
                        beforeSend: function () {
                            layer.load();
                        },
                        success: function (res) {
                            if (res.authenticated) {
                                localStorage.setItem("userId", res.principal.id);
                                localStorage.setItem("username", res.principal.username);
                                localStorage.setItem("nickname", res.principal.nickname);

                                var authorities = res.authorities;
                                var permissions = [];
                                for (var i in authorities) {
                                    permissions.push(authorities[i].authority);
                                }
                                localStorage.setItem("permissions", JSON.stringify(permissions));

                                layer.msg("登录成功", {time: 500}, function () {
                                    location.href = '/';
                                });
                            } else {
                                layer.msg("登录失败，请重试");
                            }
                        },
                        error: function (xhr) {
                            var msg = xhr.responseText;
                            var response = JSON.parse(msg);
                            layer.msg(response.retMsg);
                        }
                    });
                    layer.closeAll();
                    return false;
                });
            });
        })
    </script>
</body>
</html>